package com.example.fresheveryday.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fresheveryday.po.Product;
import com.example.fresheveryday.restful.JsonResult;
import com.example.fresheveryday.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.fresheveryday.po.Product;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ll
 * @since 2023-07-5
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService productService;
    @RequestMapping("/list")
    public List<Product> list(@RequestBody(required = false) Map<String,Object> map){
        String id = map.get("id").toString();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("categoryId",id);
        System.out.println("typeId:" + id);
        System.out.println(productService.list(wrapper));

        List<Product> products = productService.list(wrapper);
        products.stream()
                .map(product -> {
                    // 过滤掉不需要的字段
                    product.setDetail(null);  // 设置不需要的字段为null
                    return product;
                })
                .collect(Collectors.toList());
        return products;
    }
    @RequestMapping("/one")
    public Product one(int id){
        System.out.println(id);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        System.out.println("productId:" + id);
        System.out.println(productService.getOne(wrapper));
        return productService.getOne(wrapper);
    }
    @RequestMapping("saveOrUpdate")
    public JsonResult save(@RequestBody Product product){
        System.out.println("修改" + product);
        productService.saveOrUpdate(product);
        return JsonResult.success("success");
    }
    @RequestMapping("listByConditions")
    public IPage<Product> listByConditions(@RequestBody Map map){
        System.out.println(map.get("categoryId").toString());
        System.out.println(map.get("name").toString());
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if(map.get("categoryId")==""){
            wrapper.like("name",map.get("name"));
        }
        else if(map.get("name")==""){
            wrapper.eq("categoryId",map.get("categoryId"));
        }
        else{
            wrapper.eq("categoryId",map.get("categoryId"))
                    .like("name",map.get("name"));
        }

        Page<Product> page = new Page<>();
        page.setCurrent((int)map.get("pageNo"))
                .setTotal(productService.countByConditions(wrapper))
                .setSize((int)map.get("pageSize"));
        return productService.getProductByConditions(page,wrapper);
    }
    @RequestMapping("del")
    public JsonResult del(int id){
        System.out.println("删除的商品id" + id);
        productService.removeById(id);
        return JsonResult.success("success");
    }
    @RequestMapping("/newrecommend")
    public List<Product> newrecommend(int id){
        List<Product> products = productService.newRecommend(id);
        products.stream()
                .map(product -> {
                    // 过滤掉不需要的字段
                    product.setDetail(null);  // 设置不需要的字段为null
                    return product;
                })
                .collect(Collectors.toList());
        return products;
    }
    @RequestMapping("/addView")
    public JsonResult add(int id){
        Product product = productService.getById(id);
        product.setViewcount(product.getViewcount() + 1);
        productService.updateById(product);
        return JsonResult.success("success");
    }
    @RequestMapping("/keshihuaView")
    public List<Product> get(){
        return productService.getFifthView();
    }
}
